﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coomtools.commclass
{

    public enum FileExtension
    {
        JPG = 255216,
        GIF = 7173,
        PNG = 13780,
        SWF = 6787,
        RAR = 8297,
        ZIP = 8075,
        _7Z = 55122,
        VALIDFILE = 9999999
        // 255216 jpg;  

        // 7173 gif;  

        // 6677 bmp,  

        // 13780 png;  

        // 6787 swf  

        // 7790 exe dll,  

        // 8297 rar  

        // 8075 zip  

        // 55122 7z  

        // 6063 xml  

        // 6033 html  

        // 239187 aspx  

        // 117115 cs  

        // 119105 js  

        // 102100 txt  

        // 255254 sql


    }
    public static class FileoperHelper
    {
        /// <summary>
        /// 获得文件的真实.jpg、.gif格式文件类的文件类型，然而对于.txt文件却不是102100，每个.txt返回的值也不一样。。
        /// </summary>
        /// <param name="filePath">jpg文件路径</param>
        /// <returns>文件扩展名字符串，没有收录则返回字符串“ErrorFile”</returns>
        public static string GetJPGFileExtension(string jpgfilePath)
        {
            //FileExtension FileExetion=new FileExtension();

            FileStream stream = new FileStream(jpgfilePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);
            string fileclass = "";
            byte buffer;
            try
            {

                buffer = reader.ReadByte();
                fileclass = buffer.ToString();
                buffer = reader.ReadByte();
                fileclass += buffer.ToString();
                stream.Close();

            }
            catch (Exception)
            {
                stream.Close();
                throw;
            }
            int fileclassnumber = Convert.ToInt32(fileclass);
            switch ( fileclassnumber )
            {
               case 255216:
                    fileclass = "jpg";
                    break;
                case 7173:
                    fileclass = "gif";
                    break;

                case 6677:
                    fileclass = "bmp";
                    break;

                default:
                    fileclass = "ErrorFile";
                    break;
            }


            return fileclass;
        }
       
        
        
        
        /// <summary>
        /// 检查真实文件是否是文本文件，不依赖扩展名
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool CheckIsTextFile(string TxtfileName)
        {
            FileStream fs = new FileStream(TxtfileName, FileMode.Open, FileAccess.Read);
            bool isTextFile = true;
            try
            {
                int i = 0;
                int length = (int)fs.Length;
                byte data;
                while (i < length && isTextFile)
                {
                    data = (byte)fs.ReadByte();
                    isTextFile = (data != 0);
                    i++;
                }
                return isTextFile;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
   


    }
}
